import unittest

from buondua.models import db_proxy, MODELS, Album, Picture

from peewee import DoesNotExist, SqliteDatabase


class TestDatabase(unittest.TestCase):

    def setUp(self):
        self.test_db = SqliteDatabase(':memory:')
        db_proxy.initialize(self.test_db)
        self.test_db.connect(reuse_if_open=True)
        self.test_db.create_tables(MODELS)

        self.test_album = Album(
            album_url='http://example.com/test',
            title='Test',
            title_slug='test',
            tags='test1,test2'
        )
        self.test_album.save()

        self.test_picture = Picture(
            url='http://example.com/test/1.jpeg',
            pic_num=1,
            album_id=self.test_album
        )
        self.test_picture.save()

    def tearDown(self):
        self.test_db.drop_tables(MODELS)
        self.test_db.close()

    def test_connect(self):
        self.assertFalse(self.test_db.is_closed())

    def test_get_non_exist_album(self):
        url = 'http://example.com/non-exist-in-database'
        self.assertEqual(
            len(
                Album.select().where(Album.album_url == url))
            ,
            0
        )

    def test_get_exist_album(self):
        url = 'http://example.com/test'
        self.assertEqual(
            len(
                Album.select().where(Album.album_url == url))
            ,
            1
        )

    def test_album_model(self):
        album_data = {
            'album_url': 'http://example.com/article1',
            'title': 'Example Article 1',
            'title_slug': 'example-article-1',
            'tags': 'tag1,tag2'
        }
        instance = Album(**album_data)
        instance.save()

        # get
        instance2 = Album.get_by_id(instance.id)
        self.assertNotEqual(id(instance), id(instance2))
        self.assertEqual(instance, instance2)
        self.assertEqual(instance.title, instance2.title)

        # update
        instance.title = 'Example Article 2'
        instance.save()
        self.assertEqual(instance.title, 'Example Article 2')
        instance2 = instance2.get_by_id(instance.id)
        self.assertEqual(instance2.title, 'Example Article 2')

        # delete
        instance.delete_instance()
        self.assertRaises(
            DoesNotExist,
            instance2.get_by_id,
            instance2.id
        )

    def test_picture_model(self):
        # create
        picture_data = {
            'url': 'http://example.com/test/2.jpeg',
            'pic_num': 2,
            'album_id': self.test_album
        }
        instance = Picture(**picture_data)
        instance.save()
        self.assertEqual(instance.url, 'http://example.com/test/2.jpeg')

        # get
        instance2 = Picture.get_by_id(instance.id)
        self.assertNotEqual(id(instance), id(instance2))
        self.assertEqual(instance, instance2)
        self.assertEqual(instance.url, instance2.url)

        # update
        instance.url = 'http://example.com/test/3.jpeg'
        instance.save()
        self.assertEqual(instance.url, 'http://example.com/test/3.jpeg')
        instance2 = instance2.get_by_id(instance.id)
        self.assertEqual(instance2.url, 'http://example.com/test/3.jpeg')

        # delete
        instance.delete_instance()
        self.assertRaises(
            DoesNotExist,
            instance2.get_by_id,
            instance2.id
        )
